/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import models.Agenda_Announcement;
import models.Course;
import models.User;
import org.hibernate.Session;
import service.AnnouncementDao;
import service.CourseDao;
import service.HibernateUtil;
import service.UserDao;

/**
 *
 * @author Serife
 */
public class AgendaController extends HttpServlet {

    private HibernateUtil HU = new HibernateUtil();
    private Session sessie = HU.openSession();
    private CourseDao cd = new CourseDao(sessie);
    private AnnouncementDao adao = new AnnouncementDao(sessie);

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        HttpSession session = request.getSession(true);
        //get the course id and display announcements
        if (request.getParameter("sc") != null) { //SC SELECTED COURSE
            Long item = Long.parseLong(request.getParameter("sc"));
            Course course = cd.get(item);
            Set<Agenda_Announcement> announcements = course.getAnnouncements();

            request.setAttribute("announcements", announcements);
            request.setAttribute("selectedCourse", course);

            if (request.getParameter("sa") != null) { //SA SELECTED Announcement
                Agenda_Announcement announcement = adao.get(Integer.parseInt(request.getParameter("sa")));
                request.setAttribute("selectedAnnouncement", announcement);
            }

            forward(request, response);

        } else {
            response.sendRedirect("agendaManager.jsp");
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {

        String title = "",
                content = "",
                author = "",
                course = "";

        if (request.getParameter("title") != null) {
            title = request.getParameter("title");
        }
        if (request.getParameter("content") != null) {
            content = request.getParameter("content");
        }

        //hidden meegegeven fields
        author = request.getParameter("author");
        course = request.getParameter("course");

        UserDao ud = new UserDao(sessie);
        User user = new User();
        for (Cookie cookie : request.getCookies()) { //krijg user object van ingelogde persoon
            if (cookie.getName().equals("id")) {
                user = ud.get(Long.parseLong(cookie.getValue()));
            }
        }

        Agenda_Announcement announcement = new Agenda_Announcement();
        announcement.setAuthor(user);
        announcement.setTitle(title);
        announcement.setContent(content);
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        //get current date time with Date()
        Date date = new Date();
        announcement.setAnnouncementDate(date);

        adao.save(announcement);

        CourseDao cdao = new CourseDao(sessie);
        Course courseObj = cdao.get(Long.parseLong(course));
        courseObj.addAnnouncement(announcement);
        cdao.update(courseObj);

        //deel om announcement naar alle studenten en eventuele docenten te sturen

        EmailController email = new EmailController();
        try {
            for (User sendTo : courseObj.getUsers()) {
                if (sendTo.getStatus().equals("Student")) {
                    email.sendEmail(sendTo.getEmail(), content, title);
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }

        forward(request, response);
    }

    protected void forward(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String address = "/agendaManager.jsp";
        RequestDispatcher dispatcher = request.getRequestDispatcher(address);
        dispatcher.forward(request, response);
    }
}
